package com.example.hangzhou.welbeing.mytestdemoapp.base;

import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hangzhou.welbeing.mytestdemoapp.R;
import com.example.hangzhou.welbeing.mytestdemoapp.adpater.MainBaseFragmentAdapter;
import com.example.hangzhou.welbeing.mytestdemoapp.view.CustomViewPager;
import com.example.hangzhou.welbeing.mytestdemoapp.view.IsNetworkUtils;
import com.example.hangzhou.welbeing.mytestdemoapp.view.MyStatusView;
import com.example.hangzhou.welbeing.mytestdemoapp.view.StatusLayout;

/**
 * 杭州惟君医疗科技有限公司10
 * Created by 20433 on 2018/2/27
 */

public abstract class MainBaseActivity extends BeseActivity {


    LinearLayout container;
    MyStatusView statusView;
    StatusLayout statusLayout;
    CustomViewPager activityMainBaseContentCustomViewPager;
    TabLayout activityMainBaseContentTablayout;
    LinearLayout activityMainBaseContentLl;
    BottomNavigationView bottomNavigationView;
    private MainBaseFragmentAdapter mainBaseFragmentAdapter;
    private String[] titles = {"主页", "群组", "商城", "用户"};
    private int[] iconSelector = {R.drawable.navigation_home_pager,R.drawable.navigation_cicle,R.drawable.navigation_mall,R.drawable.navigation_user};
    /**
     * @desc 主要内容的布局资源填充进来
     */
    @Override
    protected View setMainContentView() {
        View view = View.inflate(this, R.layout.activity_main_base_content, null);
        findViewMain(view);
        setSupportActionBar(toolbar);
        // multistateView();
        return view;
    }

    /**
     * @desc 多状态视图
     */
    private void multistateView() {
        statusView = MyStatusView.getInstance(this, "就是没有数据怎么招吧", new MyStatusView.onRetryClickLister() {
            @Override
            public void onRetryClick() {
                Toast.makeText(MainBaseActivity.this, "重新加载数据", Toast.LENGTH_LONG).show();
                statusLayout.showLoading();
            }
        });
        statusLayout = new StatusLayout.Builder().setContentView(activityMainBaseContentLl).setStatusView(statusView).build();
        if (!IsNetworkUtils.isNetworkConnected(this)) {  //没有网络
            statusLayout.showSetting();
        }
    }

    /**
     * @desc 初始化数据 onCreate()
     */
    @Override
    protected void initView() {
        mainBaseFragmentAdapter = new MainBaseFragmentAdapter(getSupportFragmentManager(), this);
        activityMainBaseContentCustomViewPager.setAdapter(mainBaseFragmentAdapter);
        activityMainBaseContentCustomViewPager.setScanScroll(false);
        //绑定
        activityMainBaseContentTablayout.setupWithViewPager(activityMainBaseContentCustomViewPager);
        activityMainBaseContentCustomViewPager.setCurrentItem(0);
        setTabs(activityMainBaseContentTablayout,this.getLayoutInflater(),titles,iconSelector);
        init();
        hiddenReturn();
    }



    private void findViewMain(View view) {
        activityMainBaseContentCustomViewPager = (CustomViewPager) view.findViewById(R.id.activity_main_base_content_customViewPager);
        activityMainBaseContentTablayout = (TabLayout) view.findViewById(R.id.activity_main_base_content_tablayout);
        activityMainBaseContentLl = (LinearLayout) view.findViewById(R.id.activity_main_base_content_ll_01);
    }
    /**
     * @description: 设置添加Tab
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, String[] tabTitlees, int[] tabImgs){
        for (int i = 0; i < tabImgs.length; i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            View view = inflater.inflate(R.layout.tab_custom,null);
            tab.setCustomView(view);
            if (i == 0) {
                // 设置第一个tab的TextView是被选择的样式
                tab.getCustomView().findViewById(R.id.tv_tab).setSelected(true);//第一个tab被选中
                tab.getCustomView().findViewById(R.id.img_tab).setSelected(true);//第一个tab被选中
            }
            TextView tvTitle = (TextView)view.findViewById(R.id.tv_tab);
            tvTitle.setText(tabTitlees[i]);
            ImageView imgTab = (ImageView) view.findViewById(R.id.img_tab);
            imgTab.setImageResource(iconSelector[i]);
        }
    }

    /**
     * @desc 初始化工作
     */
    protected abstract void init();

    /**
     * @desc 显示内容页面
     */
    public void showContent() {
        statusLayout.showContent();
    }

    /**
     * @desc 显示空数据页面
     */
    public void showEmpty() {
        statusLayout.showEmpty();
    }

    /**
     * @desc 显示加载错误页面
     */
    public void showRetry() {
        statusLayout.showRetry();
    }

    /**
     * @desc 显示网络错误页面
     */
    public void showSetting() {
        statusLayout.showSetting();
    }

    /**
     * @desc 显示正在加载页面
     */
    public void showLoading() {
        statusLayout.showLoading();
    }

}
